package com.xrit.sunnyday.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.sunnyday.dao.PermissionDao;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        // 使用PageHelper.startPage
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 有查询条件的处理, 模糊查询
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            // 拼接%
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        // 紧接着的查询会被分页
        Page<Permission> page = permissionDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增权限
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }


    /**
     * 删除权限
     */
    @Override
    public void deleteById(int id){
        // 先删除权限与角色的关系
        permissionDao.deleteRolePermission(id);
        // 删除权限
        permissionDao.deleteById(id);
    }

    /**
     * 通过id查询权限
     */
    @Override
    public Permission findById(int id) {
        return permissionDao.findById(id);
    }

    /**
     * 更新权限
     */
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }
}
